<template>
  <div class="com-container">
    <div class="com-chart" ref="seller_ref"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getSeller } from "@/api";
export default {
  data() {
    return {
      sellerData: null,
      echartsDom: null,
      pageNum: 1, //第几页
      totalPage: 0, //总共多少页
      timeId: null, //定时器
    };
  },
  computed: {
    ...mapState(["style"]),
  },
  mounted() {
    this.initEachat();
    this.getData();
    this.screenAdapter();
    window.addEventListener("resize", this.screenAdapter);
  },
  watch: {
    style() {
      console.log("主题切换了");
      this.echartsDom.dispose(); // 销毁当前的图表
      this.initEachat(); // 重新以最新的主题名称初始化图表对象
      this.screenAdapter(); // 完成屏幕的适配
      this.upData(); // 更新图表的展示
    },
  },
  methods: {
    // 获取数据
    async getData() {
      let data = await getSeller();
      this.sellerData = data.sort((a, b) => a.value - b.value);

      this.totalPage =
        this.sellerData.length % 5 === 0
          ? this.sellerData.length / 5
          : this.sellerData.length / 5 + 1;
      this.upData();
      this.startIntVal();
    },
    // 初始化Eachats元素
    initEachat() {
      this.echartsDom = this.$echarts.init(this.$refs.seller_ref, this.style); //第一个参数是dom元素，第二个是主题
      const initOption = {
        title: {
          text: "▎商家销售统计",
          left: 20,
          top: 20,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "line",
            z: 0,
            lineStyle: {
              color: "#2D3443",
              type: "solid",
            },
          },
        },
        xAxis: {
          type: "value",
        },
        yAxis: {
          type: "category",
        },
        // 图表位置
        grid: {
          top: "20%",
          left: "3%",
          right: "6%",
          bottom: "3%",
          containLabel: true, // 距离是包含坐标轴上的文字
        },
        series: [
          {
            label: {
              show: true,
              position: "right",
              textStyle: {
                color: "#fff",
              },
            },
            type: "bar",
            itemStyle: {
              color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                // 百分之0状态之下的颜色值
                {
                  offset: 0,
                  color: "#5052EE",
                },
                // 百分之100状态之下的颜色值
                {
                  offset: 1,
                  color: "#AB6EE5",
                },
              ]),
            },
          },
        ],
      };
      this.echartsDom.setOption(initOption);
      this.echartsDom.on("mouseover", () => {
        clearInterval(this.timeId);
      });
      this.echartsDom.on("mouseout", () => {
        this.startIntVal();
      });
    },
    // 更新页面元素
    upData() {
      let showData = this.sellerData.slice(
        (this.pageNum - 1) * 5,
        this.pageNum * 5
      );
      // 获取y轴数据
      let yDatanames = showData.map((item) => item.name);
      //   获取X轴数据
      let yDatas = showData.map((item) => item.value);
      const initOption = {
        yAxis: {
          data: yDatanames,
        },
        series: [
          {
            data: yDatas,
          },
        ],
      };
      this.echartsDom.setOption(initOption);
    },
    // 浏览器大小变化
    screenAdapter() {
      const titleFontSize = (this.$refs.seller_ref.offsetWidth / 100) * 3.6;
      console.log(this.$refs.seller_ref.offsetWidth);
      let adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontSize,
            },
          },
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0],
            },
          },
        ],
      };
      this.echartsDom.setOption(adapterOption);
      this.echartsDom.resize();
    },
    // 初始化定时器
    startIntVal() {
      if (this.timeId) clearInterval(this.timeId);
      this.timeId = setInterval(() => {
        this.pageNum++;
        if (this.pageNum > this.totalPage) this.pageNum = 1;
        this.upData();
      }, 3000);
    },
  },
};
</script>

<style lang="scss" scoped>
</style>